
package de.uni.wuppertal.server.util;

import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;

import javax.swing.ImageIcon;

/**
 * @author Martin Schröder
 * @author Patrik Nordsiek
 */
public abstract class Resource
{
	private static final String PATH_BASE = "/de/uni/wuppertal/server/resource/";
	
	public static URL getURL(String location)
	{
		return Resource.class.getResource(PATH_BASE + location);
	}
	
	public static File getFile(String location)
	{
		try
		{
			return new File(getURL(location).toURI());
		}
		catch(URISyntaxException e)
		{
			return null;
		}
	}
	
	public static String getText(String location, String characterSet)
	{
		try
		{
			StringBuffer buffer = new StringBuffer();
			
			BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(getFile(location)), characterSet));
			String line;
			
			while(null != (line = reader.readLine()))
			{
				buffer.append(line);
			}
			
			reader.close();
			
			return buffer.toString();
		}
		catch(Exception e)
		{
			return null;
		}
	}
	
	public static ImageIcon getIcon(String location)
	{
		return new ImageIcon(getURL(location));
	}
	
	public static InputStream getInputStream(String location)
	{
		return Resource.class.getResourceAsStream(PATH_BASE + location);
	}
}
